﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;

using Ivy.Common.Framework.Base;
using Ivy.Common.Framework.DataAccess;
using Ivy.UserRegistration.Entity;

namespace Ivy.UserRegistration.DataAccess
{
    public class UserProfileImageDao : IDaoInterface<UserProfileImage, UserProfileImageCriteria>
    {
        #region  Variable & constant
        #endregion  Variable & constant

        #region Constructor & Destructor
        #endregion Constructor & Destructor

        #region Properties
        #endregion Properties

        #region Public Methods
        public bool Insert(UserProfileImage UserProfileImageEntity)
        {
            bool IsOperationSuccessful = false;
            if (UserProfileImageEntity.EntityState == EntityState.New)
            {
                DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
                DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
                CmdObj.CommandText = "Dasideen_UserProfileImage_Insert";
                CmdObj.CommandType = CommandType.StoredProcedure;

                TranslateContentMetaDataMasterFromEntityToDatabaseParameters(CmdObj, UserProfileImageEntity);

                DatabaseCommon.InsertStoredProcForParameterDatEntityBase(CmdObj, UserProfileImageEntity);

               
                DataAccessProviderUtility.AddParameter(CmdObj, "@Sys_DateOfCreation", ParameterDirection.Output, DbType.DateTime, DBNull.Value);



                CmdObj.Connection = DbConn;
                DbConn.Open();
                CmdObj.Prepare();
                int _NoOfRecord = CmdObj.ExecuteNonQuery();
                if (_NoOfRecord == 1)
                {
                   
                    UserProfileImageEntity.Sys_DateOfCreation = Convert.ToDateTime(CmdObj.Parameters["@Sys_DateOfCreation"].Value);
                    UserProfileImageEntity.Sys_DateOfModification = UserProfileImageEntity.Sys_DateOfCreation;
                    UserProfileImageEntity.EntityState = EntityState.UnChanged;
                    IsOperationSuccessful = true;
                }
                CmdObj.Dispose();
                DbConn.Close();
            }
            else
            {

                throw new Exception("For Inserting new UserProfileImage entity data to database,[EntityState] attribute should be [New]");
            }

            return IsOperationSuccessful;
        }

        public bool Update(UserProfileImage UserProfileImageEntity)
        {
            bool IsOperationSuccessful = false;

            if (UserProfileImageEntity.EntityState == EntityState.Changed)
            {
                DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
                DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
                CmdObj.CommandText = "Dasideen_UserProfileImage_Update";
                CmdObj.CommandType = CommandType.StoredProcedure;

                DataAccessProviderUtility.AddParameter(CmdObj, "@UserKey", ParameterDirection.Input, DbType.Int32, UserProfileImageEntity.UserKey);

                TranslateContentMetaDataMasterFromEntityToDatabaseParameters(CmdObj, UserProfileImageEntity);
                DatabaseCommon.UpdateStoredProcForParameterDatEntityBase(CmdObj, UserProfileImageEntity);

                DataAccessProviderUtility.AddParameter(CmdObj, "@Sys_DateOfModification", ParameterDirection.Output, DbType.DateTime, DBNull.Value);

                CmdObj.Connection = DbConn;
                DbConn.Open();
                CmdObj.Prepare();
                int _NoOfRecords = CmdObj.ExecuteNonQuery();
                if (_NoOfRecords == 1)
                {
                    UserProfileImageEntity.Sys_DateOfModification = Convert.ToDateTime(CmdObj.Parameters["@Sys_DateOfModification"].Value);
                    UserProfileImageEntity.EntityState = EntityState.UnChanged;
                    IsOperationSuccessful = true;
                }

                CmdObj.Dispose();
                DbConn.Close();
            }
            else
            {
                throw new Exception("For Updating existing UserProfileImage entity data to database, [EntityState] attribute should be [Changed]");
            }

            return IsOperationSuccessful;
        }

        public bool Delete(UserProfileImage DataEntity)
        {
            throw new NotImplementedException();
        }

        public UserProfileImage GetByID(string ID)
        {
            throw new NotImplementedException();
        }

        public List<UserProfileImage> GetAll()
        {
            throw new NotImplementedException();
        }

        public List<UserProfileImage> GetByCriteria(UserProfileImageCriteria CriteriaEntity)
        {
            throw new NotImplementedException();
        }

        public UserProfileImage GetByUserKey(int UserKey)
        {
            UserProfileImage _SelectedUserProfileImage = null; //IMPORTANT NOTE:- Just declare the variable never create an instance, create instance only when required.

            DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
            DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
            CmdObj.CommandText = "Dasideen_UserProfileImage_GetByUserKey";
            CmdObj.CommandType = CommandType.StoredProcedure;

            DataAccessProviderUtility.AddParameter(CmdObj, "@UserKey", ParameterDirection.Input, DbType.Int32, Convert.ToInt32(UserKey));
            

            CmdObj.Connection = DbConn;
            DbConn.Open();
            CmdObj.Prepare();
            DbDataReader _DataReader = CmdObj.ExecuteReader();

            if (_DataReader.HasRows)
            {
                if (_DataReader.Read())
                {
                    _SelectedUserProfileImage = new UserProfileImage(); //IMPORTANT NOTE:- Create instance only when required.
                    TranslateUserFieldsFromReaderToEntity(_SelectedUserProfileImage, _DataReader);
                }
            }
            //else
            //{
            //    throw new Exception("No UserProfileImage found");
            //}
            CmdObj.Dispose();
            DbConn.Close();

            return _SelectedUserProfileImage;
        }
        #endregion Public Methods

        #region Events
        #endregion Events

        #region Private Methods
        private void TranslateUserFieldsFromReaderToEntity(UserProfileImage pUserProfileImage, DbDataReader pDbDataReader)
        {

            pUserProfileImage.UserKey = Convert.ToInt32(pDbDataReader["UserKey"]);
            pUserProfileImage.UserLoginID = Convert.ToString(pDbDataReader["UserLoginID"]);
            pUserProfileImage.UserImageData = (Byte[])(pDbDataReader["UserProfileImageData"]);
            pUserProfileImage.UserImageType = Convert.ToString(pDbDataReader["UserImageType"]);
            pUserProfileImage.UserImageFileName = Convert.ToString(pDbDataReader["UserImageFileName"]);

            DatabaseCommon.TranslateSystemFieldsFromReaderToEntity(pUserProfileImage, pDbDataReader);

        }
        private void TranslateContentMetaDataMasterFromEntityToDatabaseParameters(DbCommand pDbCommand, UserProfileImage pUserProfileImageEntity)
        {
            DataAccessProviderUtility.AddParameter(pDbCommand, "@UserKey", ParameterDirection.Input, DbType.Int32, pUserProfileImageEntity.UserKey);
            DataAccessProviderUtility.AddParameter(pDbCommand, "@UserLoginID", ParameterDirection.Input, DbType.String, pUserProfileImageEntity.UserLoginID);
            DataAccessProviderUtility.AddParameter(pDbCommand, "@UserProfileImageData", ParameterDirection.Input, DbType.Binary, pUserProfileImageEntity.UserImageData);
            DataAccessProviderUtility.AddParameter(pDbCommand, "@UserImageType", ParameterDirection.Input, DbType.String, pUserProfileImageEntity.UserImageType);
            DataAccessProviderUtility.AddParameter(pDbCommand, "@UserImageFileName", ParameterDirection.Input, DbType.String, pUserProfileImageEntity.UserImageFileName);
        }
        #endregion Private Methods
        
    }
}
